package com.cloud.storage.controller;

import com.cloud.storage.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description
 * @Author lhj
 * @Date 2024/9/1 14:38
 */
@RestController
@RequestMapping("/api/v1/file")
public class FileUploadController {
    
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/file-upload", method = RequestMethod.POST)
    public ResponseEntity<?> uploadFile(@RequestParam(value = "file",required = false) MultipartFile file) {
        String fileName = fileUploadService.uploadFile(file);
        return ResponseEntity.ok("File uploaded successfully file name: " + fileName);
    }

    /**
     * 文件预览
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/preview-file", method = RequestMethod.GET)
    public ResponseEntity<?> previewFile(@RequestParam(value = "fileName") String fileName) {
        String previewFileUrl = fileUploadService.previewFile(fileName);
        return ResponseEntity.ok(previewFileUrl);
    }
    
    
    
    
    
}
